﻿using System;
using System.Collections.Generic;
using System.Text;
using WeShare.WeiXin.Common;
using WeShare.WeiXin.Mp.Other.Entity;
using static WeShare.WeiXin.Common.OpenApi;
using static WeShare.WeiXin.Common.HttpUtil;
#if !NET40
using System.Threading.Tasks;
#endif
namespace WeShare.WeiXin.Mp.Other
{
    public class OtherApi
    {
        /// <summary>
        /// 获取微信服务器ip
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <param name="isCache">是否缓存</param>
        /// <returns></returns>
        public static IpListRes GetWxServerIp(bool isCache = true, string tokenOrAppId = "")
        {
            var config = GetConfig(tokenOrAppId);
            if (config != null && isCache)
            {
                var list = CacheService.Get<List<string>>(config.AppId + "_serverip");
                if (list != null)
                {
                    return new IpListRes { IpLst = list };
                }
            }
            var url = $"https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={GetToken(tokenOrAppId)}";
            var res = GetResult<IpListRes>(url);
            if (res.errcode == 0)
            {
                if (config != null) CacheService.Add(config.AppId + "_serverip", res.IpLst, 360000);
            }

            return res;
        }
        /// <summary>
        /// 清除接口限额。每个帐号每月共10次清零操作机会
        /// </summary>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static BaseRes ClearQuota(string appid = "")
        {
            var config = GetConfig(appid);
            if (config != null)
            {
                var url = $"https://api.weixin.qq.com/cgi-bin/clear_quota?access_token={GetToken(appid)}";
                return PostResult<BaseRes>(new { appid = config.AppId }, url);
            }
            return new BaseRes { errcode = -1, errmsg = "appid不存在" };
        }
        /// <summary>
        /// 获取公众号的自动回复规则
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static AutoReplyRule GetAutoreplyInfo(string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token={GetToken(tokenOrAppId)}";
            return GetResult<AutoReplyRule>(url);
        }
        /// <summary>
        /// 长链接转短链接接口
        /// </summary>
        /// <param name="longUrl"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static ShortUrlRes ToShortUrl(string longUrl, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/shorturl?access_token={GetToken(tokenOrAppId)}";
            var data = new
            {
                action = "long2short",
                long_url = longUrl
            };
            return PostResult<ShortUrlRes>(data, url);
        }
#if !NET40

        /// <summary>
        /// 长链接转短链接接口
        /// </summary>
        /// <param name="longUrl"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<ShortUrlRes> ToShortUrlAsync(string longUrl, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/shorturl?access_token={await GetTokenAsync(tokenOrAppId)}";
            var data = new
            {
                action = "long2short",
                long_url = longUrl
            };
            return await PostResultAsync<ShortUrlRes>(data, url);
        }


        /// <summary>
        /// 获取微信服务器ip
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <param name="isCache">是否缓存</param>
        /// <returns></returns>
        public static async Task<IpListRes> GetWxServerIpAsync(bool isCache = true, string tokenOrAppId = "")
        {
            var config = GetConfig(tokenOrAppId);
            if (config != null && isCache)
            {
                var list = CacheService.Get<List<string>>(config.AppId + "_serverip");
                if (list != null)
                {
                    return new IpListRes { IpLst = list };
                }
            }
            var url = $"https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={await GetTokenAsync(tokenOrAppId)}";
            var res = await GetResultAsync<IpListRes>(url);
            if (res.errcode == 0)
            {
                if (config != null) CacheService.Add(config.AppId + "_serverip", res.IpLst, 360000);
            }

            return res;
        }
        /// <summary>
        /// 清除接口限额。每个帐号每月共10次清零操作机会
        /// </summary>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static async Task<BaseRes> ClearQuotaAsync(string appid = "")
        {
            var config = GetConfig(appid);
            if (config != null)
            {
                var url = $"https://api.weixin.qq.com/cgi-bin/clear_quota?access_token={await GetTokenAsync(appid)}";
                return await PostResultAsync<BaseRes>(new { appid = config.AppId }, url);
            }
            return new BaseRes { errcode = -1, errmsg = "appid不存在" };
        }
        /// <summary>
        /// 获取公众号的自动回复规则
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<AutoReplyRule> GetAutoreplyInfoAsync(string tokenOrAppId)
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token={await GetTokenAsync(tokenOrAppId)}";
            return await GetResultAsync<AutoReplyRule>(url);
        }
#endif
    }
}
